<template>
  <div class="container-comment">
    <el-card>
      <div slot="header">
        <my-bread>评论管理</my-bread>
      </div>
      <!-- 表格 -->
      <el-table :data="comments">
        <el-table-column label="文章标题" prop="title" width="300px"></el-table-column>
        <el-table-column label="总评论数" prop="total_comment_count"></el-table-column>
        <el-table-column label="粉丝评论数" prop="fans_comment_count"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">{{scope.row.comment_status?'已打开':'已关闭'}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              @click="toggleStatus(scope.row)"
              v-if="scope.row.comment_status"
              type="danger"
              size="small"
            >关闭评论</el-button>
            <el-button @click="toggleStatus(scope.row)" v-else type="success" size="small">打开评论</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px"
        background
        layout="prev, pager, next"
        @current-change="changePager"
        :page-size="reqParams.per_page"
        :current-page="reqParams.page"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'comment-page',
  data () {
    return {
      // 评论管理列表数据
      comments: [],
      // 总条数
      total: 0,
      // 参数对象
      reqParams: {
        page: 1,
        per_page: 20,
        response_type: 'comment'
      }
    }
  },
  created () {
    this.getComments()
  },
  methods: {
    // 切换文章的评论状态
    toggleStatus (row) {
      // 弹出确认框，且提示和状态有关
      const text1 = '此操作会开启该文章的评论功能，您确认吗？'
      const text2 = '此操作会关闭该文章的评论功能，关闭后用户将无法对该文章进行评论，您确认吗？'
      this.$confirm(row.comment_status ? text2 : text1, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            // 点击的确认
            const {
              data: { data }
            } = await this.$http.put(`/comments/status?article_id=${row.id}`, {
              allow_comment: !row.comment_status
            })
            // data.allow_comment  修改后文章的评论状态
            this.$message.success(data.allow_comment ? '打开评论成功' : '关闭评论成功')
            // 更新列表当前文章状态
            row.comment_status = data.allow_comment
          } catch (e) {
            this.$message.error('操作失败')
          }
        })
        .catch(() => {})
    },
    // 实现分页
    changePager (newPage) {
      this.reqParams.page = newPage
      this.getComments()
    },
    // 获取评论列表数据
    async getComments () {
      const {
        data: { data }
      } = await this.$http.get('/articles', { params: this.reqParams })
      this.comments = data.results
      this.total = data.total_count
    }
  }
}
</script>

<style scoped lang='less'></style>